package cn.congzhou.crm.web.controller;

import cn.congzhou.crm.domain.SystemDictionaryItem;
import cn.congzhou.crm.query.SystemDictionaryItemQueryObject;
import cn.congzhou.crm.service.ISystemDictionaryItemService;
import cn.congzhou.crm.service.ISystemDictionaryService;
import cn.congzhou.crm.utils.ResultJSON;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import javafx.geometry.Pos;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {

    private Logger logger = LoggerFactory.getLogger(SystemDictionaryItemController.class);

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") SystemDictionaryItemQueryObject qo, Model model) {
        PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemService.selectByPage(qo);
        model.addAttribute("ro",pageInfo);
        model.addAttribute("dictionaries",systemDictionaryService.selectAll());
        return "/systemDictionaryItem/list";
    }


    @RequestMapping("/input")
    @RequiresPermissions(value = {"编辑字典明细","systemDictionaryItem:input"},logical = Logical.OR)
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("entity", systemDictionaryItemService.selectByPrimaryKey(id));
        }

        return "/systemDictionaryItem/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"保存/修改字典明细","systemDictionaryItem:saveOrUpdate"},logical = Logical.OR)
    @ResponseBody
    public Object saveOrUpdate(SystemDictionaryItem sysDicItem) {
        ResultJSON result = new ResultJSON();
        try {
            systemDictionaryItemService.saveOrUpdate(sysDicItem);

        } catch (Exception e) {
            e.printStackTrace();
            result.mark("保存失败");
        }

        return result;
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"删除字典明细","systemDictionaryItem:delete"},logical = Logical.OR)
    @ResponseBody
    public Object delete(Long id) {
        ResultJSON result = new ResultJSON();
        try {
            systemDictionaryItemService.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("删除失败");
        }

        return result;
    }

    //==========================添加ligerUI添加的方法============================================//

    @RequestMapping(value = "/toSystemDictionaryItemGrid",method = RequestMethod.GET)
    @RequiresPermissions(value = {"查询字典明细","systemDictionaryItem:query"},logical = Logical.OR)
    public String toSystemDictionaryItemGrid(Long parentId,Model model){
        model.addAttribute("parentId",parentId);
        return "/systemDictionaryItem/systemDictionaryItemGrid";
    }

    //@ResponseBody
    @RequestMapping(value = "/getSystemDictionaryItemData",method = RequestMethod.POST)
    @RequiresPermissions(value = {"查询字典明细","systemDictionaryItem:query"},logical = Logical.OR)
    public Object getSystemDictionaryItemData(@ModelAttribute("qo") SystemDictionaryItemQueryObject qo, HttpServletResponse response){
        PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemService.selectByPage(qo);
        String json = JSON.toJSONString(pageInfo);
        PrintWriter writer = null;
        try{
            writer = response.getWriter();
            writer.print(json);
            writer.flush();
        }catch (Exception e){
            logger.error("查询/响应字典明细数据出错",e);
        }finally {
            if(writer != null){
                writer.close();
            }
        }
        return null;
    }


}
